/**
 * 
 * Copyright (C) 2011 Cody Stoutenburg . All rights reserved.
 *
 *       This program is free software; you can redistribute it and/or
 *       modify it under the terms of the GNU Lesser General Public License
 *       as published by the Free Software Foundation; either version 2.1
 *       of the License, or (at your option) any later version.
 *
 *       This program is distributed in the hope that it will be useful,
 *       but WITHOUT ANY WARRANTY; without even the implied warranty of
 *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *       GNU Lesser General Public License for more details.
 *
 *       You should have received a copy of the GNU Lesser General Public License
 *       along with this program; if not, write to the Free Software
 *       Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
 * 
 */
package ca.usherbrooke.view.slick.jade;

import org.newdawn.slick.Animation;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.geom.Rectangle;

import ca.usherbrooke.model.entity.IEntityModel;
import ca.usherbrooke.tools.position.Vector2D;

/**
 * @author Cody Stoutenburg
 * 
 */
public class EntityViewJade {
	private final Animation spriteUp;
	private final Animation spriteDown;
	private final Animation spriteRight;
	private final Animation spriteLeft;

	private static final Double PI_4 = Math.PI / 4.0;
	private static final Double PI_34 = 3 * Math.PI / 4.0;

	private final Long entityId;
	private final IWorldProvider worldProvider;

	public EntityViewJade(Animation up, Animation down, Animation right,
			Animation left, Long entityId, IWorldProvider model) {

		spriteUp = up;
		spriteUp.setAutoUpdate(true);

		spriteDown = down;
		spriteRight = right;
		spriteLeft = left;

		this.entityId = entityId;
		this.worldProvider = model;

	}

	public Long getGuid() {
		return entityId;
	}

	public IEntityModel getEntity() {
		return worldProvider.getWorld().getEntity(entityId);
	}

	private Animation getCurrentAnimation() {
		Animation anim = spriteLeft;
		Vector2D direction = getEntity().getRealSpeed();

		if (PI_4 < direction.getAt() && direction.getAt() < PI_34) {
			anim = spriteDown;
		} else if (-PI_4 > direction.getAt() && direction.getAt() > -PI_34) {
			anim = spriteUp;
		} else if (PI_4 >= direction.getAt() && direction.getAt() >= -PI_4) {
			anim = spriteRight;
		}
		return anim;
	}

	public void render(GameContainer container, Graphics g) {
		IEntityModel entity = getEntity();
		if (entity == null) {
			return;
		}

		Rectangle position = entity.getSize();
		if (entity.getRealSpeed().getSquaredMagnitude() > 0) {
			getCurrentAnimation().setAutoUpdate(true);
		} else {
			getCurrentAnimation().setAutoUpdate(false);
		}

		g.drawAnimation(getCurrentAnimation(), (int) position.getX() - 2,
				(int) position.getY() - 5);
		g.drawRect(position.getX(), position.getY(), position.getWidth(),
				position.getHeight());

		g.drawString(entity.getGuid().toString(), position.getX(),
				position.getY());

		// test jump animation
		if (entity.isJumping()) {
			getCurrentAnimation().setAutoUpdate(false);
		} else {
			getCurrentAnimation().setAutoUpdate(true);
		}

	}
}
